package com.luo.chengrui.module.sa.service.matrix;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import com.luo.chengrui.module.sa.service.matrix.BusinessManagerMatrixService;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.luo.chengrui.module.sa.controller.admin.matrix.vo.*;
import com.luo.chengrui.module.sa.dal.dataobject.matrix.BusinessManagerMatrixDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import com.luo.chengrui.module.sa.convert.matrix.BusinessManagerMatrixConvert;
import com.luo.chengrui.module.sa.dal.mysql.matrix.BusinessManagerMatrixMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.luo.chengrui.module.sa.enums.ErrorCodeConstants.*;;

/**
 * 职能管理矩阵  Service 实现类
 *
 * @author luodz
 */
@Service
@Validated
public class BusinessManagerMatrixServiceImpl implements BusinessManagerMatrixService {

    @Resource
    private BusinessManagerMatrixMapper businessManagerMatrixMapper;

    @Override
    public String createBusinessManagerMatrix(BusinessManagerMatrixCreateReqVO createReqVO) {
        // 插入
        BusinessManagerMatrixDO businessManagerMatrix = BusinessManagerMatrixConvert.INSTANCE.convert(createReqVO);
        validateBusinessManagerMatrixExists(null,BusinessManagerMatrixDO::getCode,createReqVO.getCode(),BUSINESS_MANAGER_MATRIX_CODE_EXISTS);
        validateBusinessManagerMatrixExists(null,BusinessManagerMatrixDO::getName,createReqVO.getName(),BUSINESS_MANAGER_MATRIX_NAME_EXISTS);
        businessManagerMatrixMapper.insert(businessManagerMatrix);
        // 返回
        return businessManagerMatrix.getId();
    }

    @Override
    public void updateBusinessManagerMatrix(BusinessManagerMatrixUpdateReqVO updateReqVO) {
        // 校验存在
        validateBusinessManagerMatrixExists(updateReqVO.getId());
        validateBusinessManagerMatrixExists(updateReqVO.getId(),BusinessManagerMatrixDO::getCode,updateReqVO.getCode(),BUSINESS_MANAGER_MATRIX_CODE_EXISTS);
        validateBusinessManagerMatrixExists(updateReqVO.getId(),BusinessManagerMatrixDO::getName,updateReqVO.getName(),BUSINESS_MANAGER_MATRIX_NAME_EXISTS);
        // 更新
        BusinessManagerMatrixDO updateObj = BusinessManagerMatrixConvert.INSTANCE.convert(updateReqVO);
        businessManagerMatrixMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessManagerMatrix(String id) {
        // 校验存在
        validateBusinessManagerMatrixExists(id);
        // 删除
        businessManagerMatrixMapper.deleteById(id);
    }

    private void validateBusinessManagerMatrixExists(String id) {
        if (businessManagerMatrixMapper.selectById(id) == null) {
            throw exception(BUSINESS_MANAGER_MATRIX_NOT_EXISTS);
        }
    }

    private void validateBusinessManagerMatrixExists(String id, SFunction<BusinessManagerMatrixDO, ?> field, Object value, final ErrorCode errorCode) {
        BusinessManagerMatrixDO entity = businessManagerMatrixMapper.selectOne(field, value);
        if (entity == null) {
            return;
        }
        if (id == null || !Objects.equals(entity.getId(), id)) {
            throw exception(errorCode);
        }
    }

    @Override
    public BusinessManagerMatrixDO getBusinessManagerMatrix(String id) {
        return businessManagerMatrixMapper.selectById(id);
    }

    @Override
    public List<BusinessManagerMatrixDO> getBusinessManagerMatrixList(Collection<String> ids) {
        return businessManagerMatrixMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BusinessManagerMatrixDO> getBusinessManagerMatrixPage(BusinessManagerMatrixPageReqVO pageReqVO) {
        return businessManagerMatrixMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BusinessManagerMatrixDO> getBusinessManagerMatrixList(BusinessManagerMatrixExportReqVO exportReqVO) {
        return businessManagerMatrixMapper.selectList(exportReqVO);
    }

}
